<?php


namespace App\Api;
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

use App\Domain\Cart as CartDomain;
use PhalApi\Api;

class Cart extends Api
{
    public function getRules()
    {
        return array(
            'getCartInfoByUid' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'min' => 1, 'desc' => '用户的id')
            ),
            'delCartById' => array(
                'userId' => array('name' => 'userId', 'require' => true, 'min' => 1, 'desc' => '用户userId')
            ),
            'insertCartByUid' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'min' => 1,   'desc' => '用户的id'),

            ),
        );
    }

    /**
     * 根据用户的id查看这辆购物车的信息
     * @desc 根据用户id，查看这两购物车的信息
     * return id为购物车对应用户的编号，用于查询添加到购物车的商品
     */
    public function getCartInfoByUid()
    {
        $user_id = $_GET['user_id'];
        $domain = new CartDomain();
        $info = $domain->getCartInfoByUid($user_id);
        return array('info' => $info);
    }

    /**
     * 删除指定用户id的购物车列表信息
     * @desc 删除指定用户id的购物车条目,返回影响行数
     *
     */
    public function delCartById()
    {
        $userId = $_GET['userId'];
        $domain = new CartDomain();
        $row = $domain->delCartById($userId);
        return array('row' => $row);

    }

    /**
     * 登录创建购物车
     * @desc 根据用户id，创建初始购物车
     * 用户登陆后，调用此接口，创建一个购物车为用户提供使用
     */
    public function insertCartByUid()
    {
        $user_id = $_GET['user_id'];
        $domain = new CartDomain();
        $id = $domain->insertCartByUid($user_id);
        return array('id' => $id);

    }
}
